package _05_树;

import _05_树.printer.BinaryTrees;
import _05_树.tree.AVLTree;
import _05_树.tree.BST;
import _05_树.tree.RBTree;
import org.junit.Test;

public class TestTree {
    @Test
    public void testBST(){
        Integer[] datas = {7,4,9,2,5,8,11,3,12,1};

        BST<Integer> bst = new BST<>();
        for(int i=0;i< datas.length;++i){
            bst.add(datas[i]);
        }
        BinaryTrees.println(bst);
        System.out.println("------------------");
       // bst.preOrderTraversal();
        System.out.println("------------------");
       // bst.inorderTraversal();
        System.out.println("------------------");
       // bst.postorderTraversal();
        System.out.println("------------------");
        bst.levelOrderTraversal(new BST.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element+" ");
                return element == 2;
            }
        });
        System.out.println("--------------------");

        System.out.println(bst.heightNR());
        System.out.println(bst.height());
        System.out.println(bst.isComplete());
    }

    @Test
    public void testBST2(){
        Integer[] datas = {7,4,9,2,5,8,11,3,12,1};

        BST<Integer> bst = new BST<>();
        for(int i=0;i< datas.length;++i){
            bst.add(datas[i]);
        }
        BinaryTrees.println(bst);
        bst.remove(7);

        BinaryTrees.println(bst);
    }

    @Test
    public void testAvl(){
        Integer[] datas = {10, 100, 42, 59, 97, 91};
        AVLTree<Integer> avl = new AVLTree<>();
        for (int i = 0; i < datas.length; i++) {
            avl.add(datas[i]);
        }
        BinaryTrees.println(avl);

        for (int i = 0; i < datas.length; i++) {
            avl.remove(datas[i]);
            System.out.println("["+datas[i]+"]");
            BinaryTrees.println(avl);
            System.out.println("---------------------");
        }
    }

    @Test
    public void testRBTree(){
        Integer[] datas = {55,87,56,74,96,22,62,20,70,68,90,50};
        RBTree<Integer> rbTree = new RBTree<>();
        for (int i = 0; i < datas.length; i++) {
            rbTree.add(datas[i]);
        }
        BinaryTrees.println(rbTree);
        for (int i = 0; i < datas.length; i++) {
            rbTree.remove(datas[i]);
            System.out.println("【"+datas[i]+"】");
            BinaryTrees.println(rbTree);
            System.out.println("------------------------");
        }
    }
}
